Doug: This is Doug Rehnstrom. Welcome to this demo
on exploring Cloud Functions. In this demo, I will show you
how to create a Cloud Function. Then we will test it
in the cloud console. Google Cloud Functions provides
a completely serverless platform for running your services. You write the code, upload it,
and it just works. You only pay per invocation
of each function. Cloud Functions
is extremely cost-effective. The first two million
requests are even free. Let's create
a Cloud Function. In the management console,
I've gone to the Cloud Functions service.
Let's click create function. When creating a Cloud Function,
give it a name and choose the region. We will name this one
"my first function." You can choose
the amount of memory allocated to the function.
Functions are written using Node.js, and you should verify
that your function executes within the amount
of memory you specify. As we'll see later, you can use
monitoring to look at memory usage. The time out determines
how long the function has to run before it terminates. A Cloud Function runs
in response to some trigger. There are three triggers. The Pub/Sub trigger responds
to a message sent to a Pub/Sub topic. The Cloud Storage trigger responds
to a change in a Cloud Storage bucket. The HTTP trigger responds to a request
made to a Cloud Function endpoint. You can think of
HTTP-triggered functions as behaving like
a single-operation API. It offers a service
to client applications that can be accessed
via some URL. Notice, if we choose HTTP trigger
for this function, the URL for that trigger
is shown down below. The sample code includes two files,
the index.js and package.json files. The package.json file
is a standard way for a Node.js application
to declare dependencies. Cloud Functions automatically download
the packages declared in this file. The index.js file contains
the invocable function which has been exported
from the file. This is the standard way
for Node.js to make functionality available
via a module specification. When you write your own functions, you can include other files
and reference them from index.js. A Cloud Storage bucket will be used
as a staging location for the code that you write.
Let's create one now. We'll call this bucket,
"cloud functions demo-dar," and we'll make it a regional bucket
in the same region as our function. Let's create the function. It'll take a couple minutes
for the function to be ready. Then we can go onto testing it. The function is now ready.
Let's click on it to test it. Notice on the general tab
you can see invocations, execution time and memory usage. These metrics will be useful
once you have functions go live and will help you tune your functions. Since this function was just created,
there are no metrics. On the trigger tab,
we can see the URL to our function. The source tab shows
the code we uploaded. The testing tab will allow us to invoke
our function with some test data. Notice how there's
a triggering event text box which includes an empty
JSON object. Let's try executing the function
with this empty object just to see what happens. No message defined. That's because our message
was not structured correctly. We can scroll down and see the logs. Notice the log entry 400.
That's a bad request. Let's try again but using
a properly formatted JSON message. Success. We can scroll down again
to look at the log entries. This time we get a log entry
with code 200. That means everything worked. In this demo,
we have created a Cloud Function that responds to an HTTP request. We've also tested that function
by sending in an HTTP request, and we've also seen
some of the management tools built into Cloud Functions that also
makes managing your functions easier. I hope you enjoyed this demo
on Google Cloud Functions. This is Doug Rehnstrom.